using System;
using Microsoft.Practices.CompositeUI.Commands;
using NUnit.Framework;
using Rhino.Mocks;
using SCSFContrib.CompositeUI.WPF.Commands;

namespace SCSFContrib.CompositeUI.WPF.UnitTest.Commands
{
	[TestFixture]
	public sealed class WrappedCabCommandTest : UnitTest
	{
		private Command _command;
		private WrappedCabCommand _wrappedCabCommand;

		protected override void SetUpCore()
		{
			base.SetUpCore();
			_command = Mocks.PartialMock<Command>();
			Mocks.ReplayAll();
			_wrappedCabCommand = new WrappedCabCommand(_command);
		}

		[Test]
		[ExpectedException(typeof(ArgumentNullException))]
		public void Constructor_ShouldThrowIfCommandIsNull()
		{
			_wrappedCabCommand = new WrappedCabCommand(null);
		}

		[Test]
		public void CanExecute_ShouldReturnTrueOnlyIfCommandIsEnabled()
		{
			_command.Status = CommandStatus.Disabled;
			Assert.IsFalse(_wrappedCabCommand.CanExecute(null));
			_command.Status = CommandStatus.Unavailable;
			Assert.IsFalse(_wrappedCabCommand.CanExecute(null));
			_command.Status = CommandStatus.Enabled;
			Assert.IsTrue(_wrappedCabCommand.CanExecute(null));
		}

		[Test]
		public void Execute_ShouldCallExecuteOnCabCommand()
		{
			Mocks.BackToRecord(_command);
			_command.Execute();
			LastCall.Repeat.Once();
			Mocks.Replay(_command);

			_wrappedCabCommand.Execute(null);
		}

		[Test]
		public void CanExecuteChanged_ShouldBeRaisedIfCabCommandChanges()
		{
			bool fired = false;

			_wrappedCabCommand.CanExecuteChanged += delegate
			{
				fired = true;
			};

			Assert.IsFalse(fired);
			InvokePrivateMethod<object>(_command, "OnChanged");
			Assert.IsTrue(fired);
		}
	}
}
